package linkedlist;

public class ListNode {
    public static final int MAX_PRINT_LENGTH = 100;
    public int val;
    public ListNode next;

    public ListNode() {
    }
    public ListNode(int val) {
        this.val = val;
    }
    public ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }

    public static void printList(ListNode head, String name) {
        System.out.print(name + ": ");
        int count = 0;
        while (head != null && count <= MAX_PRINT_LENGTH) {
            System.out.print(head.val);
            System.out.print(" -> ");
            head = head.next;
            count++;
        }
        System.out.println("null.");
    }

    public static ListNode buildList(int... vals) {
        ListNode next = null;
        for (int i = vals.length-1; i >= 0; i--) {
            ListNode curr = new ListNode(vals[i], next);
            next = curr;
        }
        return next;
    }

    public String toString() {
        return "" + this.val;
    }

    @Override
    public int hashCode() {
        return this.val;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof ListNode) {
            return this.val == ((ListNode) obj).val;
        }
        return false;
    }
}
